﻿// Hieuvm: Build Image Template
function GetImageAttribute(args)
{
    var ImageID = args.ImageID;
    var Url     = args.Url;
    var Description = args.Description;
    var Author = args.Author;
    var Source = args.Source;
    var Width  = args.Width;
    var Height = args.Height;
    var Align  = args.Align;
    var BorderWidth = args.BorderWidth;
    var BorderColor = args.BorderColor;
    var IsCancel = args.IsCancel;
    var IsUsingTemplate = args.IsUsingTemplate;                
                     
    var strObj = '';

    strObj += '<img alt="' + Description + '" ';
    strObj += ' title="' + Description + '" ';
    strObj += ' src="' + Url + '" ';
    strObj += ' style="width:' + Width + 'px; height:' + Height + 'px; border: solid ' + BorderWidth + 'px ' + BorderColor + ';" ';    
   
    // Float Style                       
    if(Align != '')
    {
        strObj += ' align=\'' + Align + '\'';
    }            
    if((Width != '') && (Width != '0'))
    {
        strObj += ' width=\'' + Width + '\'';
    }
    if((Height != '') && (Height != '0'))
    {
        strObj += ' height=\'' + Height + '\'';
    }            
    
    strObj += ' />';

    if (IsUsingTemplate) {
        var _style = "align:\"center\" class=\"ImgBoxEmbeddedCenter\"";
        var _className = "EditorBorder";

        if (Align.toLowerCase() == "left") {
            _style = "align=\"left\" class=\"ImgBoxEmbeddedLeft\"";
            _className = "EditorBorderLeft";
        }

        if (Align.toLowerCase() == "right") {
            _style = "align=\"right\" class=\"ImgBoxEmbeddedRight\"";
            _className = "EditorBorderRight";
        }

        var _style1 = '<p>';
        _style1 += '<table border="0" cellspacing="0" cellpadding="0" style="width:' + Width + 'px" ' + _style + '">';
        _style1 += '<tr>';
        _style1 += '<td>';
        _style1 += '<div>';
        //_style1 +=                      '<a href="/ImageHandler.ashx?ImageID=' + ImageID + '" rel="lyteshow[vacation]">';
        _style1 += strObj;
        //_style1 +=                      '</a>';

        // Check Caption is null
        if (Description != "") {
            _style1 += '<div class="pCaption" style="width:100%" >';
            _style1 += Description;
            _style1 += '</div>';
        }

        // Check Author is null
        if (Author != "") {
            _style1 += '<div class="pImgAuthor" style="width:100%" >';
            _style1 += Author;
            _style1 += '</div>';
        }

        // Check Source is null  
        if (Source != "") {
            _style1 += '<div class="pSource" style="width:100%" >';
            _style1 += Source;
            _style1 += '</div>';
        }

        _style1 += '</div>';
        _style1 += '</td>';
        _style1 += '</tr>';
        _style1 += '</table>';
        _style1 += '</p>';

        strObj = _style1;
    }
                
    return strObj;
}

function GetDocumentAttribute(args) {
    var linkUrl = args.linkUrl;
    var linkTarget = args.linkTarget;
    var linkName = args.linkName;
    var IsCancel = args.IsCancel;

    var strObj = '';
    strObj += '<a href="' + linkUrl + '" target="' + linkTarget + '" />' + linkName + '</a>';
    return strObj;
}  

// Hieuvm: Build Video Template
function GetVideoAttribute(args)
{
    var VideoID = args.VideoID;
    var Url     = args.Url;
    var Description = args.Description;
    var Author = args.Author;
    var Source = args.Source;
    var Width  = args.Width;
    var Height = args.Height;
    var Align  = args.Align;
    var IsCancel = args.IsCancel;
    var IsUsingTemplate = args.IsUsingTemplate;

    //if (Width.value == '' || Height.value == '') {
        Width.value = '300';
        Height.value = '275';
    //}
    
    var strObj ='';

    // Build Embedded Code

    
//    strObj =  '<object width="' + Width +'" height="' + Height +'" type="application/x-shockwave-flash" data="/Players/player.swf">' ;
//    strObj += '<param name="allowfullscreen" value="true">';
//    strObj += '<param name="allowscriptaccess" value="always">';
//    strObj += '<param name="wmode" value="opaque">';
//    strObj += '<param name="flashvars" value="file=' + Url +'">';

    strObj += ' <embed type="application/x-shockwave-flash" src="/Players/Player.swf" ';
    strObj += ' allowfullscreen="true" ';
    strObj += ' allowscriptaccess="always" ';
    strObj += ' wmode="opaque" ';
    strObj += ' flashvars="file=' + Url + '" ';
    strObj += ' style="width:' + Width + 'px height:' + Height + 'px" width="' + Width + '" height="' + Height + '"></embed>';
    
//    strObj += '</object>';
    // Build Frame
    var _style = "align=\"center\" class=\"ImgBoxEmbeddedCenter\"";
        
    if(Align.toLowerCase() == "left")
    {
        _style = "align=\"left\" class=\"ImgBoxEmbeddedLeft\"";        
    }
    
    if(Align.toLowerCase() == "right")       
    {      
        _style = "align=\"right\" class=\"ImgBoxEmbeddedRight\"";        
    }
    
    var _style1 ='<p>';
    _style1 +=       '<table border="0" cellspacing="0" cellpadding="0" style="width:' + Width  + 'px" ' + _style + '>';
    _style1 +=          '<tr>';                
    _style1 +=              '<td>';
    _style1 +=                  '<div>';
    _style1 +=                          strObj;        
     
    if(IsUsingTemplate)
    { 
        // Check Caption is null            
        if(Description != "")
        {
            _style1 +=                  '<div class="pCaption" style="width:100%" >';
            _style1 +=                      Description;
            _style1 +=                  '</div>';
        }
        
        // Check Author is null  
        if(Author != "")
        {
            _style1 +=                  '<div class="pImgAuthor" style="width:100%" >';
            _style1 +=                      Author;
            _style1 +=                  '</div>';
        }

        // Check Source is null  
        if (Source != "") {
            _style1 += '<div class="pSource" style="width:100%" >';
            _style1 += Source;
            _style1 += '</div>';
        }
    }
    
    _style1 +=                  '</div>';
    _style1 +=              '</td>';                        
    _style1 +=          '</tr>';        
    _style1 +=      '</table>';
    _style1 +='</p>';
                           
    strObj = _style1;
    
    return strObj;
} 

//Hieuvm: Build Audio Template
function GetAudioAttribute(args)
{    
    var VideoID = args.VideoID;
    var Url     = args.Url;
    var Description = args.Description;
    var Author = args.Author;
    var Source = args.Source;
    var Width  = args.Width;
    var Height = args.Height;
    var Align  = args.Align;
    var IsCancel = args.IsCancel;
    var IsUsingTemplate = args.IsUsingTemplate;

    //if (Width.value == '' || Height.value == '') {
        Width.value = '300';
        Height.value = '24';
    //}
    
    var strObj ='';

//    strObj = '<object width="' + Width + '" height="' + Height + '" type="application/x-shockwave-flash" data="/Players/player.swf">';
//    strObj += '<param name="allowfullscreen" value="true">';
//    strObj += '<param name="allowscriptaccess" value="always">';
//    strObj += '<param name="wmode" value="opaque">';
//    strObj += '<param name="flashvars" value="file=' + Url + '">';

    strObj += ' <embed type="application/x-shockwave-flash" src="/Players/Player.swf" ';
    strObj += ' allowfullscreen="true" ';
    strObj += ' allowscriptaccess="always" ';
    strObj += ' wmode="opaque" ';
    strObj += ' flashvars="file=' + Url + '" ';
    strObj += ' style="width:' + Width + 'px height:' + Height + 'px" width="' + Width + '" height="' + Height + '"></embed>';
    
//    strObj += '</object>';
           
    // Build Frame
    var _style = "align=\"center\" class=\"ImgBoxEmbeddedCenter\"";
        
    if(Align.toLowerCase() == "left")
    {
        _style = "align=\"left\" class=\"ImgBoxEmbeddedLeft\"";        
    }
    
    if(Align.toLowerCase() == "right")       
    {      
        _style = "align=\"right\" class=\"ImgBoxEmbeddedRight\"";        
    }
    
    var _style1 ='<p>';
    _style1 +=       '<table border="0" cellspacing="0" cellpadding="0" style="width:' + Width  + 'px" ' + _style + '>';
    _style1 +=          '<tr>';                
    _style1 +=              '<td>';
    _style1 +=                  '<div>';
    _style1 +=                          strObj;        
     
    if(IsUsingTemplate)
    { 
        // Check Caption is null            
        if(Description != "")
        {
            _style1 +=                  '<div class="pCaption" style="width:100%" >';
            _style1 +=                      Description;
            _style1 +=                  '</div>';
        }
        
        // Check Author is null  
        if(Author != "")
        {
            _style1 +=                  '<div class="pImgAuthor" style="width:100%" >';
            _style1 +=                      Author;
            _style1 +=                  '</div>';
        }

        // Check Source is null  
        if (Source != "") {
            _style1 += '<div class="pSource" style="width:100%" >';
            _style1 += Source;
            _style1 += '</div>';
        }        
    }
    
    _style1 +=                  '</div>';
    _style1 +=              '</td>';                        
    _style1 +=          '</tr>';        
    _style1 +=      '</table>';
    _style1 +='</p>';

    strObj = _style1;

    return strObj;    
}

var ImageRegPattern = /\?ImageID=\s*\d{0,9}|\?Imag#9ID=\s*\d{0,9}|\?ImageID=\s*\d{0,9}/gi;

// Get HeadImageID by Newest Image in Article
function GetHeadImageIDInEditor()
{
    var strContent = radEditor.get_html();
    
    var HeadImageID = "";    
    var MatchResult = strContent.match(ImageRegPattern);
    if(MatchResult != null)
    {
        // Get First Selection
        HeadImageID = GetImageID(MatchResult[0]);
    }
    
    return HeadImageID;
}

// Save Article-Image (Save all Images that embedded in the Article)
function ParseListImageIDInEditor(strContent)
{
    var temp = FindFileID(ImageRegPattern, strContent);            
    hdfArrFileID.value = temp.trimEnd();
}

// Find All Image Embedded in Article Content
function BuildImageIDList(ImageRegPattern, strContent)
{
    var MatchResult = strContent.match(ImageRegPattern);
    var ArrImageID = '';
    
    if(MatchResult != null)
    {
        for(var i = 0 ; i < MatchResult.length ; i++)
        {
            ArrImageID += GetFileID(MatchResult[i]) != '' ? GetFileID(MatchResult[i]) + SeparatorChar : '';
        }            
    }
    
    ArrFileID = ReduceFileID(ArrFileID);
    
    return ArrFileID;
}

// Get ImageID
function GetImageID(Query)
{
    var strQuery = Query.split('=');
    return strQuery[1].trim();
}